package com.loe.ktui

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.res.*
import com.loe.ktui.util.KtCornerProvider
import com.loe.ktui.util.UiShape

open class UiImage(id: String? = null) : UiView(id)
{
    override var bg: Any? = null

    var isOpen: Boolean? = null
    var rate: Double? = null
    var auto: Boolean? = null
    var colorFilter: Int? = null
    var scaleType: ImageView.ScaleType? = null
    var src: Any? = null
    var srcRes: Int? = null

    open fun initUI(context: Context, view: KtImageView)
    {
        super.initUI(context, view)

        isAlphaClick?.let { view.isAlphaClick = it }

        isOpen?.let { view.isOpen = it }
        rate?.let { view.rate = it }
        auto?.let { view.auto = it }
        colorFilter?.let { view.setColorFilter(it) }
        scaleType?.let { view.scaleType = it }
        src?.let {
            if (it is UiShape)
            {
                view.setImageDrawable(it.createDrawable())
            } else if (it is Int)
            {
                view.setImageDrawable(ColorDrawable(it))
            } else
            {
                view.src = it.toString()
            }
        }
        srcRes?.let { view.setImageResource(it) }
    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): KtImageView
    {
        return KtImageView(context).also()
        {
            initUI(context, it)
            addToParent(parent, it)
        }
    }
}

fun Any.ui_Image(id: String? = null, block: (UiImage.() -> Unit)? = null): UiImage
{
    val ui = UiImage(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}

@SuppressLint("AppCompatCustomView")
open class KtImageView : ImageView
{
    constructor(context: Context) : super(context)
    {
        initXML()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(context.obtainStyledAttributes(attrs, R.styleable.KtImageView))
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(context.obtainStyledAttributes(attrs, R.styleable.KtImageView, defStyleAttr, 0))
    }

    private fun initXML(typedArray: TypedArray? = null)
    {
        if (background == null) setBackgroundColor(Color.TRANSPARENT)
        typedArray?.let()
        {
            if (it.hasValue(R.styleable.KtImageView_kt_corner))
            {
                val corner = it.getDimensionOrThrow(R.styleable.KtImageView_kt_corner).toInt()
                val cornerDirect = KtDirects[it.getInt(R.styleable.KtImageView_kt_cornerDirect, 4)]

                outlineProvider = KtCornerProvider(corner, cornerDirect, this)
            }
            if (it.hasValue(R.styleable.KtImageView_kt_isAlphaClick))
            {
                isAlphaClick = it.getBooleanOrThrow(R.styleable.KtImageView_kt_isAlphaClick)
            }
            if (it.hasValue(R.styleable.KtImageView_kt_link))
            {
                link = it.getStringOrThrow(R.styleable.KtImageView_kt_link)
            }

            if (it.hasValue(R.styleable.KtImageView_kt_isOpen))
            {
                isOpen = it.getBooleanOrThrow(R.styleable.KtImageView_kt_isOpen)
            }
            if (it.hasValue(R.styleable.KtImageView_kt_rate))
            {
                rate = it.getFloatOrThrow(R.styleable.KtImageView_kt_rate).toDouble()
            }
            if (it.hasValue(R.styleable.KtImageView_kt_auto))
            {
                auto = it.getBooleanOrThrow(R.styleable.KtImageView_kt_auto)
            }
            if (it.hasValue(R.styleable.KtImageView_kt_colorFilter))
            {
                val colorFilter = it.getColorOrThrow(R.styleable.KtImageView_kt_colorFilter)
                setColorFilter(colorFilter)
            }

            it.recycle()
        }
    }

    /******************************************** 原生 ******************************************/

    /** 固定比例 */
    var rate = -1.0

    /** 是否自动高度 */
    var auto = false

    var urlString = ""

    var pathString = ""

    val displayString get() = pathString.ifEmpty { urlString }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
    {
        if (drawable == null)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            return
        }
        // 计算出ImageView的宽度
        val viewWidth = MeasureSpec.getSize(widthMeasureSpec)
        if (auto)
        {
            // 根据图片长宽比例算出ImageView的高度
            val viewHeight = viewWidth * drawable.intrinsicHeight / drawable.intrinsicWidth
            // 将计算出的宽度和高度设置为图片显示的大小
            setMeasuredDimension(viewWidth, viewHeight)
        } else if (rate > 0)
        {
            // 将计算出的宽度和高度设置为图片显示的大小
            setMeasuredDimension(viewWidth, (viewWidth * rate).toInt())
        } else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        }
    }

    /********************************************** 点击 ****************************************/

    var link = ""
        set(value)
        {
            field = value
            setKtDelayClickListener { KtUi.onLink?.invoke(context, value) }
        }

    var isOpen = false
        set(value)
        {
            field = value
            if (value)
            {
                setOnClickListener()
                {
                    KtUi.onImageOpen?.invoke(this, displayString)
                }
            } else
            {
                setOnClickListener(null)
            }
        }

    var isAlphaClick = false
        set(value)
        {
            field = value
            isClickable = value
        }

    override fun dispatchSetPressed(pressed: Boolean)
    {
        if (isAlphaClick) alpha = if (pressed || !isEnabled) 0.75f else 1f
        super.dispatchSetPressed(pressed)
    }

    override fun setEnabled(enabled: Boolean)
    {
        if (isAlphaClick) alpha = if (enabled) 1f else 0.75f
        super.setEnabled(enabled)
    }
}